package com.argrace.smart.base;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.argrace.smart.R;

/**
 * Created by argrace_dev_1 on 2018/1/29.
 */

public class BaseActivity extends AppCompatActivity {
    private ViewFlipper mContentView;
    private RelativeLayout head_left_rl;
    private ImageView head_left_img;
    private RelativeLayout head_right_rl;
    private ImageView head_right_img;
    private TextView head_title;
    BroadcastReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        reqpermission();
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_layout_base);
        // 初始化公共头部
        mContentView = (ViewFlipper) super.findViewById(R.id.layout_container);
        initTitleView();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 竖屏
        ActivityManager.addActivity(this, this.getClass().getName());
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                JSONObject obj = JSON.parseObject(intent.getExtras().getString("msg"));
                dorecieve(obj);
            }
        };
        IntentFilter filter = new IntentFilter("com.example.broadcast");
        registerReceiver(receiver, filter);
    }

    private void reqpermission() {

        PackageManager pm = getPackageManager();

        // 所需的全部权限
        final String[] BASE_PERMISSIONS = new String[]{
                android.Manifest.permission.CAMERA//摄像头
                , android.Manifest.permission.READ_PHONE_STATE
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
                , Manifest.permission.READ_EXTERNAL_STORAGE
        };

        boolean permission = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission(BASE_PERMISSIONS[1], this.getPackageName()));
        if (permission) {

        } else {
            ActivityCompat.requestPermissions(this, BASE_PERMISSIONS, 0);
        }

    }

    protected void dorecieve(JSONObject obj) {
        //是否做统一筛选

    }

    protected void initTitleView() {

        head_left_rl = (RelativeLayout) findViewById(R.id.head_activity_left_rl);
        head_left_img = (ImageView) findViewById(R.id.head_activity_left_img);
        head_right_rl = (RelativeLayout) findViewById(R.id.head_activity_right_rl);
        head_right_img = (ImageView) findViewById(R.id.head_activity_right_img);
        head_title = (TextView) findViewById(R.id.head_activity_title);
        head_left_rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ActivityManager.updateActivity(this.getClass().getName(), ConstantConfigure.ACTIVITY_STATE_ONSTOP);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        ActivityManager.updateActivity(this.getClass().getName(), ConstantConfigure.ACTIVITY_STATE_ONPAUSE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActivityManager.updateActivity(this.getClass().getName(), ConstantConfigure.ACTIVITY_STATE_ONRESUME);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        setContentView(view);
    }

    @Override
    public void setContentView(View view) {

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        mContentView.addView(view, lp);
        setTitle(true, false, "");

    }

    protected void showLeft(boolean isshow, Integer resid) {
        if (isshow) {
            head_left_img.setVisibility(View.VISIBLE);
        } else {
            head_left_img.setVisibility(View.GONE);
        }
        if (resid != null) {
            head_left_img.setImageResource(resid);
        }
    }

    protected void showRight(boolean isshow, Integer resid) {
        if (isshow) {
            head_right_img.setVisibility(View.VISIBLE);
        } else {
            head_right_img.setVisibility(View.GONE);
        }
        if (resid != null) {
            head_right_img.setImageResource(resid);
        }
    }

    protected void setTitle(String title) {
        if (title != null) {
            head_title.setText(title);
        }
    }

    /**
     * boolean isshowleft是否显示左侧按钮
     * boolean isshowright是否显示右侧按钮
     * title标题名
     */
    protected void setTitle(boolean isshowleft, boolean isshowright, String title) {
        showLeft(isshowleft, null);
        showRight(isshowright, null);
        setTitle(title);
    }

    protected void setLeftandRightClickListeners(View.OnClickListener left, View.OnClickListener right) {
        if (left != null) {
            head_left_rl.setOnClickListener(left);
        }
        if (right != null) {
            head_right_rl.setOnClickListener(right);
        }
    }

    protected void toastActBtm(String str) {
        Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    }
}
